home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 19 / madtrb40.zip / IXTON.INC < prev    next >
Text File  |  1986-01-21  |  2KB  |  47 lines

  1. Function IXtoN(x : integer;   n : integer) : integer;
  2. {  This include file contains the x_to_n function that computes  }
  3. {  the Nth power of an integer number, where N is an integer.    }
  4. {  Author :  Herb Holden                                         }
  5. {  Date   :  11/17/84                                            }
  6. {  Application : All systems.                                    }
  7. {                                                                }
  8. {  This function was taken from TUG newsletter vol I, issue 5    }
  9. {  pg. 32.                                                       }
  10. {  Modified to be a pure integer function by Claire A. Rinehart  }
  11.  
  12. Var
  13.   p : integer;
  14.   y : real;
  15. Begin               {computes x to the nth power}
  16.   if x = 0 then
  17.     begin
  18.       if n = 0 then
  19.         begin       {0 to the 0 is undefined}
  20.           writeln('** error in IxTOn:  x=0, n=0 **');
  21.            y := 1/0
  22.         end
  23.       else
  24.         IxTOn := 0   {0 to the n is 0}
  25.     end
  26.   else
  27.     begin
  28.       if n < 0 then      {x to -n is (1/x) to n and can not be assigned to an integer variable}
  29.         begin
  30.           writeln('ERROR!!  Attempted to assign a fraction to an integer.');
  31.           y := 1/0;
  32.         end;
  33.       if odd(n) then     {prepare for while loop}
  34.         p := x
  35.       else
  36.         p := 1;          {x to 0 will be 1}
  37.       n := n shr 1;
  38.       while n <> 0 do
  39.         begin
  40.           x := sqr(x);
  41.           if odd(n) then p := p * x;
  42.           n := n shr 1
  43.         end;
  44.       IxTOn := p
  45.     end
  46. end;  {IxTOn}
  47.